package gui;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import domein.DomeinController;
import domein.Locatie;

public class ReisplanOverzichtResultatenPanel extends javax.swing.JPanel {

	private int yposition = -40;
	private DomeinController domeinController;
	private List<String> routes;
	private TreePath[] locaties;
	private Map<String, JLabel> labelsRoutes;
	private Map<String, JLabel> labelsRoutesVolgorde;
	private List<Locatie> listLocaties;
	
	private JLabel label;
	
	public ReisplanOverzichtResultatenPanel(DomeinController domeinController, List<String> routes, TreePath[] locaties) {
		super();
		this.domeinController = domeinController;
		this.routes = routes;
		this.locaties = locaties;
		this.labelsRoutes = new HashMap<String, JLabel>();
		this.labelsRoutesVolgorde = new HashMap<String, JLabel>();
		this.listLocaties = new ArrayList<Locatie>();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(600, 390));
			this.setLayout(null);
			{
				for (TreePath path : locaties) {
				
					TreeNode[] nodes = {(TreeNode)path.getPathComponent(0), (TreeNode)path.getPathComponent(1),(TreeNode)path.getPathComponent(2),(TreeNode)path.getPathComponent(3)};
					listLocaties.add(domeinController.selecteerLocatie((nodes)));
				}
				
				label = new JLabel();
				this.add(label);
				label.setBounds(20, yposition, 200, 24);
				label.setText("klsqdfjqsdkjf");
				
				Iterator<String> iterator = routes.iterator();
				while (iterator.hasNext()) {
					yposition += 60;
					String route = iterator.next();
					List<Locatie> volgordeLocaties = new ArrayList<Locatie>();
					
					if(route.equals("Kortste Route"))
						volgordeLocaties = domeinController.berekenKortsteRoute(listLocaties);
					if(route.equals("Langste Route"))
						volgordeLocaties = domeinController.berekenLangsteRoute(listLocaties);
					if(route.equals("Droogste Route"))
						volgordeLocaties = domeinController.berekenDroogsteRoute(listLocaties);
					if(route.equals("Natste Route"))
						volgordeLocaties = domeinController.berekenNatsteRoute(listLocaties);					
					if(route.equals("Koudste Route"))
						volgordeLocaties = domeinController.berekenKoudsteRoute(listLocaties);
					if(route.equals("Warmste Route"))
						volgordeLocaties = domeinController.berekenWarmsteRoute(listLocaties);
					
					{
						labelsRoutes.put(route, new JLabel());
						this.add(labelsRoutes.get(route));
						labelsRoutes.get(route).setBounds(20, yposition, 200, 24);
						labelsRoutes.get(route).setText(route);
					}
					
					if(volgordeLocaties != null){
						Iterator iterators = volgordeLocaties.iterator();
						String stringRoute = "";
						
						while(iterators.hasNext()){
							stringRoute += ((Locatie)iterators.next()).getNaamLocatie();
							if(iterators.hasNext()){
								stringRoute += " -> ";
							}
						}
						
						System.out.println(stringRoute);
						
						{
							labelsRoutesVolgorde.put(route, new JLabel());
							this.add(labelsRoutesVolgorde.get(route));
							labelsRoutesVolgorde.get(route).setBounds(20, yposition+30, 200, 24);
							labelsRoutesVolgorde.get(route).setText(stringRoute);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
